package com.zj.utils.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zj.utils.R;
import com.zj.utils.utils.utilcode.KeyboardUtils;


/**
 * 自定义头部View
 *
 * @author jiang zhu on 2019/10/7
 */
public final class TitleBar extends RelativeLayout implements View.OnClickListener {

    private Context mContext;
    public TextView mTitleTv;
    private ImageView mImgBack;
    private ImageView mImgRight;
    private TextView mTxtRight;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();


    }

    /**
     * 初始化布局
     */
    private void initView() {
        //加载布局
        inflate(mContext, R.layout.layout_title, this);

        //控制头布局，返回关闭页面
        mImgBack = findViewById(R.id.imgBack);
        //控制标题
        mTitleTv = findViewById(R.id.txtTitle);
        //右边图片
        mImgRight = findViewById(R.id.imgRight);
        //右边文字
        mTxtRight = findViewById(R.id.txtRight);

        mImgBack.setOnClickListener(this);
    }

    /**
     * 设置标题栏标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        mTitleTv.setText(title);
    }


    /**
     * 设置返回按钮图片
     *
     * @param imageId 图片id
     */
    public void setBackImage(int imageId) {
        if (imageId != 0) {
            mImgBack.setImageResource(imageId);
        }
    }


    /**
     * 设置返回按钮图片是否显示
     *
     * @param imageVisiable 是否显示
     */
    public void setBackImageVisiable(boolean imageVisiable) {
        if (imageVisiable) {
            mImgBack.setVisibility(VISIBLE);
        } else {
            mImgBack.setVisibility(GONE);
        }
    }


    /**
     * 设置右边图片
     *
     * @param imageId 图片id
     */
    public void setRightImage(int imageId) {
        if (imageId != 0) {
            if (mTxtRight.getVisibility() == VISIBLE) {
                throw new IllegalArgumentException("文字和图片不可同时设置");
            }
            mImgRight.setVisibility(View.VISIBLE);
            mImgRight.setImageResource(imageId);
        }
    }

    /**
     * 设置右边文字
     *
     * @param text 文字
     */
    public void setRightText(String text) {
        if (text != null) {
            if (mImgRight.getVisibility() == VISIBLE) {
                throw new IllegalArgumentException("文字和图片不可同时设置");
            }
            mTxtRight.setVisibility(View.VISIBLE);
            mTxtRight.setText(text);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.imgBack) {
            //关闭页面
            if (KeyboardUtils.isSoftInputVisible(((Activity) mContext)))
                KeyboardUtils.hideSoftInput(this);
            ((Activity) mContext).finish();
        }
    }

    public void setRightTextOnClickListener(OnClickListener onClickListener) {
        mTxtRight.setOnClickListener(onClickListener);
    }
}
